package com.jvtd.eyl.widget;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.allen.library.SuperTextView;
import com.jvtd.eyl.R;
import com.jvtd.widget.custom.JvtdCustomRelativeLayout;
/**
 * 编辑栏
 *
 * 作者:chenlei
 * 时间:2018/11/14 9:18 AM
 */
public class EditorView extends JvtdCustomRelativeLayout implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private OnEditorListener mOnEditorListener;

    private SuperTextView mTextView;
    private CheckBox mCheckBox;

    public static final int SUB = 1;
    public static final int ADD = 2;

    public EditorView(Context context) {
        super(context);
    }

    public EditorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EditorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View v) {
        if (mOnEditorListener != null)
            mOnEditorListener.onClick(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (mOnEditorListener != null)
            mOnEditorListener.onSelectAll(isChecked);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.view_editor;
    }

    @Override
    public void bindView(View view, @Nullable AttributeSet attrs) {
        mTextView = findViewById(R.id.editor_btn);
        mCheckBox = findViewById(R.id.check_box);

        mTextView.setOnClickListener(this);
        mCheckBox.setOnCheckedChangeListener(this);
    }

    public interface OnEditorListener{
        void onSelectAll(boolean all);
        void onClick(View view);
    }

    /**
     * 设置图片
     * @param resId 图片id
     */
    public void setImage(@DrawableRes int resId){
        mTextView.setCenterTvDrawableLeft(ContextCompat.getDrawable(mContext,resId));
    }

    //还原
    public void reset(){
        mCheckBox.setChecked(false);
    }

    //设置当天数量即墨市
    public void setNum(int num,int type){
        if (num == 0)
            reset();
        if (type == SUB)
            mTextView.setCenterString(String.format(mContext.getString(R.string.editor_delete_title), num));
        else
            mTextView.setCenterString(String.format(mContext.getString(R.string.editor_add_title), num));
    }

    public void setOnEditorListener(OnEditorListener onEditorListener) {
        mOnEditorListener = onEditorListener;
    }
}
